﻿using MediatR;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using S.Common.ApiResult;
using S.Common.helper;
using S.Weaning.Domain.Dto;
using static S.Weaning.Read.ApI.Application.Command.MaternalWeaningCommand;

namespace S.Weaning.Read.ApI.Controllers
{
    /// <summary>
    /// 种母断奶读
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class MaternalWeaningController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly IConfiguration configuration;
        private readonly IHttpContextAccessor httpContextAccessor;

        public MaternalWeaningController(IMediator mediator,IConfiguration configuration, IHttpContextAccessor httpContextAccessor)
        {
            this.mediator = mediator;
            this.configuration = configuration;
            this.httpContextAccessor = httpContextAccessor;
        }
        /// <summary>
        /// 种母断奶
        /// </summary>
        /// <param name="ddate"></param>
        /// <param name="ear"></param>
        /// <param name="build"></param>
        /// <param name="userid"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public async Task<ApiResult<List<MaternalWeaningDto>>> ShowMaternalMating(string? ddate, string? ear, string? build, int userid,int page,int size)
        {
            var list = await mediator.Send(new ShowMaternalWeaning(ddate, ear, build, userid, page,size));
            list.StatusCode = 200;
            list.Message = "success";
            list.Timestamp = DateTime.Now.ToString("yyyyMMddHHmmss");
            return list;
        }
        /// <summary>
        /// 反填种母
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetFieldModel(string ear)
        {
            string Gateway = configuration.GetValue<string>("Gateway");
            var jsonString = HttpClientHelper.GetResponse(Gateway + "/ParturitionRead/api/Parturition/GetLambList");
            var response = JsonConvert.DeserializeObject<List<LambListDto>>(jsonString).Where(t => t.MotherEar == ear);
            return Ok(response);
        }

        /// <summary>
        /// 反填种母断奶信息（用于修改）
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
            public async Task<IActionResult> MaternalById(int id)
            {
            var model = await mediator.Send(new MaternalByIdCommand(id));
            return Ok(model);
        }
        /// <summary>
        /// 断奶种母下拉
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpGet]
        public IActionResult GetEarModel()
        {
            string Gateway = configuration.GetValue<string>("Gateway");
            var jsonString = HttpClientHelper.GetResponse(Gateway + "/ParturitionRead/api/Parturition/GetParturitionEarTag");
            var list = JsonConvert.DeserializeObject<List<LamDto>>(jsonString);
            return Ok(list);
        }

        public class LamDto 
        {
                  public int Value { get; set; }

                  public string Label { get; set; }
        }

    }
}
